package epsi.mobile.helpers;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class GPSTracker extends Service
{
 
		private LocationManager	lManager = null;
		public static String choix_source = "";
		public static double longitude, latitude, altitude;
		public static boolean isConnected = false;
		
		
		private transient LocationListener onLocationChange = new LocationListener()
		{

			 @Override
			 public void onStatusChanged(String provider, int status, Bundle extras)
			 {
				 lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, onLocationChange);
				 Criteria crit = new Criteria();
			     crit.setAccuracy(Criteria.ACCURACY_FINE);
			   	 choix_source = lManager.getBestProvider(crit, true);
			   	 
			   	 
			   	 Log.e("Loc","Provider : "+choix_source);
			   	 
			   	 if(choix_source != null && choix_source != "")
			   		 lManager.requestLocationUpdates(choix_source,200,0, onLocationChange);
			 }
	
			 @Override
			 public void onProviderEnabled(String provider)
			 {
				 
				 
				 lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				 lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, onLocationChange);
				 Criteria crit = new Criteria();
			     crit.setAccuracy(Criteria.ACCURACY_FINE);
			   	 choix_source = lManager.getBestProvider(crit, true);
			   	 
			   	 
			   	 Log.e("Loc","Provider : "+choix_source);
			   	 
			   	 if(choix_source != null && choix_source != "")
			   		 lManager.requestLocationUpdates(choix_source,200,0, onLocationChange);
			   	 else
			   		lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,200,0, onLocationChange);
			 }
	
			 @Override
			 public void onProviderDisabled(String provider)
			 {
				 isConnected = false;
				 lManager.removeUpdates(onLocationChange);
				 
			 }
	
			 @Override
			 public void onLocationChanged(Location location)
			 {
				 
				 isConnected = true;
				 

				 
				 latitude = location.getLatitude();
				 longitude = location.getLongitude();
				 altitude = location.getAltitude();
				 
				 isConnected = true;
				
			 }
		};

		 @Override
		 public IBinder onBind(Intent arg0)
		 {
			 return null;
		 }

		 @Override
		 public void onCreate()
		 {
						 
			 choix_source = new String();
			 longitude = latitude = altitude = 0;

			 lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			 lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, onLocationChange);
			 Criteria crit = new Criteria();
		     crit.setAccuracy(Criteria.ACCURACY_FINE);
		   	 choix_source = lManager.getBestProvider(crit, true);
		   	 
		   	 
		   	 Log.e("Loc","Provider : "+choix_source);
		   	 
		   	 if(choix_source != null && choix_source != "")
		   		 lManager.requestLocationUpdates(choix_source,200,0, onLocationChange);
		   	 else
		   		lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,200,0, onLocationChange);
	    	 
			   	
	    	 super.onCreate();
		 }
		

		 
		 @Override
		 public int onStartCommand(Intent intent, int flags, int startId)
		 {
		
			 return super.onStartCommand(intent, flags, startId);
		 }
		
		 @Override
		 public void onDestroy()
		 {
			 
			 isConnected = false;
			 
			 Log.i("GeoService","Exit");
			 
			 if(lManager == null)
				 Log.e("Loc","lMan null");
			 
			 if(onLocationChange == null)
				 Log.e("Loc","onLocationChange null");
			 
			 lManager.removeUpdates(onLocationChange);
			 
			 super.onDestroy();
		 }


}